﻿var _jsonData_yjz =
    {
        "status": 0,
        "desc": "操作成功",
        "data": [{
            "id": "99ABE2D4377FE96E5FE0FF4C8E2F51F1",
            "queueId": "0e1151aec8444a669b722f8f1696c4d2",
            "queuingKey": "3c3d064856df4ef9bbd6fc57f11e0803",
            "queuingOrderNo": null,
            "patientKey": "370983199111166133",
            "patientName": "赵小二",
            "registerId": "3",
            "registerTime": "20180306110842",
            "timeType": 0,
            "triageTime": "20180306110953",
            "queuingDate": "20180306000000",
            "delayTime": 0,
            "editor": "user",
            "editTime": "20180306120913",
            "isDeleted": 0,
            "sex": 1,
            "signOrderNo": null,
            "hospitalId": "4e66fabafd944e458dc259ce890c31f3",
            "remarks": null,
            "isNumRecall": 0,
            "isGreenChannel": 0,
            "treatStatus": 1,
            "treatmentStage": 4,
            "waitingStatus": 0,
            "treatmentEndStatus": 1,
            "doctorId": "3c3d064856df4ef9bbd6fc57f11e0803",
            "doctorName": "张鹏鹏",
            "callingTime": "20180306145141",
            "isReferral": 0
        }
        ]
    };


var yjz_datagridflg = false;

// 请求复诊数据
function requestYjzData(queueId) {

    localStorage.setItem("_ISNOSPLIT_PAGE_", "N");

    $.ajax({
        url: _yjzGetPatients,
        type: "get",
        data: {
            queueId: queueId
        },
        success: function (data) {

            layer.close(mLoadFlg);

            var result = JSON.parse(data);
            if (result.status == 0) {
                loadYjzDataGrid(result.data);
            } else {
                loadDataFailed(result.desc, 11);
            }
        },
        error: function (jqXHR, textStatus, errorThrown) {
            console.log(textStatus);
            console.log(errorThrown);
            loadDataFailed("获取已就诊列表数据失败!", 11);
        }
    });
    /*
        var jsonitems = _jsonData_yjz.data;
        loadYjzDataGrid(jsonitems);*/
}

//构建复诊表格结构
function loadYjzDataGrid(d /* String */) {

    //测试放开
    //d = _jsonData_hz;

    var totalNum = 0;
    var _html = "";
    var count = 0;

    var _queryKey = localStorage.getItem("_P_KEY_") == null ? "" : localStorage.getItem("_P_KEY_");
    var _initSelected = false;

    if (d !== null) {
        totalNum = d.length;
        for (var i = 0; i < d.length; i++) {
            var _id = d[i].id;
            var _fznumber = getNotBlankData(d[i].registerId);
            var _hzname = getNotBlankData(d[i].patientName);
            var _doctorname = getNotBlankData(d[i].doctorName);
            var _treate_status = getNotBlankData(getTreatStatus(d[i].treatStatus));
            var _waitstatus = getNotBlankData(getWaitingStatus(d[i].waitingStatus));
            var _treate_stage_status = getNotBlankData(getTreatmentStatus(d[i].treatmentStage));
            var _treate_end_status = getNotBlankData(getEndStatus(d[i].treatmentEndStatus));
            var _recall_status = getNotBlankData(getRecallStatus(d[i].isNumRecall));
            var _green_status = getNotBlankData(getGreenStatus(d[i].isGreenChannel));
            var _ys_edit_time = getNotBlankData(d[i].delayEditTime);
            var _ys = getNotBlankData(d[i].delayTime);
            var _wb = getTimeName(d[i].timeType);
            var _ghtm = getNotBlankData(d[i].registerTime);
            var _jhtm = getNotBlankData(d[i].callingTime);
            var _remark = getNotBlankData(d[i].remarks);
            var pretestStage = getNotBlankData(d[i].pretestStage);
            var hznameStyle = "background: #FFFFFF";
            if (showColorButtons()){
                if (pretestStage == "1") {
                    hznameStyle ="background: #fe0000";
                } else if (pretestStage == "2" ) {
                    hznameStyle ="background: #fffa08";
                } else if (pretestStage == "3" ){
                    hznameStyle ="background: #33cc00";
                }
            }
            count++;

            var _inputRadio = "";
            if (_queryKey == _id) {

                localStorage.setItem("_P_KEY_", "");
                _initSelected = true;
                _inputRadio = "<input checked='checked' type='radio' name='sel_radio' id='" + _id + "' name='yjz_" + _fznumber + "' onclick='select_yjz_row(this);' " +
                    "data-treate_status='" + d[i].treatStatus + "' data-wait_status='" + d[i].waitingStatus + "' " +
                    "data-treate_stage_status='" + d[i].treatmentStage + "' data-treate_end_status='" + d[i].treatmentEndStatus + "' " +
                    "data-recall_status='" + d[i].isNumRecall + "' data-green_status='" + d[i].isGreenChannel + "' data-color_status='" + d[i].pretestStage + "' " +
                    "data-queueId='" + d[i].queueId + "' data-patientKey='" + d[i].patientKey + "' data-doctorId='" + d[i].doctorId + "' data-doctorName='" + d[i].doctorName + "' data-signId='" + d[i].creater + "'/>";

            } else {
                _inputRadio = "<input  type='radio' name='sel_radio' id='" + _id + "' name='yjz_" + _fznumber + "' onclick='select_yjz_row(this);' " +
                    "data-treate_status='" + d[i].treatStatus + "' data-wait_status='" + d[i].waitingStatus + "' " +
                    "data-treate_stage_status='" + d[i].treatmentStage + "' data-treate_end_status='" + d[i].treatmentEndStatus + "' " +
                    "data-recall_status='" + d[i].isNumRecall + "' data-green_status='" + d[i].isGreenChannel+ "' data-color_status='" + d[i].pretestStage + "'"+
                    "data-queueId='" + d[i].queueId + "' data-patientKey='" + d[i].patientKey + "' data-doctorId='" + d[i].doctorId + "' data-doctorName='" + d[i].doctorName + "' data-signId='" + d[i].creater + "'/>";
            }


            _html += "<tr>";
            _html += "<td><span >" + _inputRadio + "</span></td>";
            _html += "<td><span style='color:#6DA4EA;'>" + count + "</span></td>";
            _html += "<td><span>" + _fznumber + "</span></td>";
            _html += "<td><span style='"+hznameStyle+"'>" + _hzname + "</span></td>";
            _html += "<td><span>" + _doctorname + "</span></td>";
            _html += "<td><span>" + _treate_status + "</span></td>";
            /*_html += "<td><span>" + _waitstatus + "</span></td>";*/
            _html += "<td><span>" + _treate_stage_status + "</span></td>";
            _html += "<td><span>" + _treate_end_status + "</span></td>";
            /*_html += "<td><span>" + _recall_status + "</span></td>";*/
            _html += "<td><span>" + _green_status + "</span></td>";
            _html += "<td><span>" + getDelayDetailTime(_ys_edit_time, _ys) + "</span></td>";
            _html += "<td><span>" + _wb + "</span></td>";
            _html += "<td><span>" + _ghtm + "</span></td>";
            _html += "<td><span>" + _jhtm + "</span></td>";
            _html += "<td style='text-overflow: ellipsis; white-space: nowrap; overflow: hidden;' title='" + _remark + "'><a data-id='" + _id + "' data-remark='" + _remark + "' onclick='set_yjz_remark(this);'><span>" + _remark + "</span></a></td>";

            _html += "</tr>";
        }
    }

    $("#t_table_yjz_content").html(_html);
    //表头排序
    $('#t_table_yjz').tablesort().data('tablesort');
    $('thead th.number').data('sortBy', function (th, td, sorter) {
        return parseInt(td.text(), 10);
    });

    //更新页面脚标
    $('#patientNum').html("当前队列总共" + totalNum + "位患者!");
    //是否有搜索，需要初始化选中效果
    if (_initSelected) {
        var _t = $("#" + _queryKey);
        select_yjz_row(_t);
    }
}

//选择一行
function select_yjz_row(t) {

    console.log($(t).prop("checked"));
    var _ischeked = $(t).prop("checked");

    $("#t_table_yjz_content tr").each(function () {
        // console.log($(this).children("td:last"));
        $(this).children("td:last").find("span").css({
            "border-right": "1px solid #FFFFFF",
//            "background-color": "#F2F3F3"
        });
    });

    //设置本行变色
    if (_ischeked) {
        console.log($(t).parent().parent().find("span"));
        $(t).parent().parent().find("span").css({
            "border-top": "1px solid #3598db",
            "border-bottom": "1px solid #3598db",
            "border-left": "1px solid #3598db",
//            "background-color": "#F2F3F3"
        });

        $(t).parent().parent().nextAll().each(function () {
            $(this).find("span").css({
                "border-top": "1px solid #3598db",
                "border-bottom": "1px solid #3598db",
//                "background-color": "#F2F3F3"
            });
        });

        $(t).parent().parent().parent().prevAll().each(function () {
            $(this).find("span").css({
                "border-top": "",
                "border-bottom": "",
                "border-left": "",
//                "background-color": "#FFFFFF"
            });
        });

        $(t).parent().parent().parent().nextAll().each(function () {
            $(this).find("span").css({
                "border-top": "",
                "border-bottom": "",
                "border-left": "",
//                "background-color": "#FFFFFF"
            });
        });
    }
    //取消本行变色
    /*else {

        $(t).parent().parent().find("span").css({
            "border-top": "",
            "border-bottom": "",
            "border-left": "",
            "background-color": "#FFFFFF"
        });

        $(t).parent().parent().nextAll().each(function () {
            $(this).find("span").css({
                "border-top": "",
                "border-bottom": "",
                "background-color": "#FFFFFF"
            });
        });
        $(t).parent().parent().nextAll().last().find("span").css({
            "border-right": "",
            "background-color": "#FFFFFF"
        });
    }*/

    // 根据选中状态，设置【按钮】的可用状态
    setButtonStatus(t);
}

// 对指定患者设置备注
function set_yjz_remark(_this) {
    var id = $(_this).attr("data-id");
    var remark = $(_this).attr("data-remark");

    var win_name = "remarkWin";
    layer.open({
        id: win_name,
        type: 2,
        title: false,
        shadeClose: false,
        closeBtn: 0,
        area: ['400px', '200px'],
        content: '../window/remark_win.html?id=' + id + "&remark=" + remark,
        btn: ['确定', '取消'],
        yes: function (index, layero) {
            var frameId = document.getElementById(win_name).getElementsByTagName('iframe')[0].id;
            var result = $('#' + frameId)[0].contentWindow.updateRemark();
            console.log(result);
            if (result != false) {
                parent.layer.close(index);
                refreshTab();
            }
        },
        cancel: function () {

        }
    });
}




